﻿package ben 
{
	import com.greensock.TweenMax;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.geom.ColorTransform;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	* ...
	* @author Ben
	*/
	public class SomeTools extends EventDispatcher
	{
		
		public function SomeTools() 
		{
			
		}
		
		private static var _bgTrame:Shape
				
		public static function getVeilSh(w:int, h:int, s:Sprite, b:Shape= null):void
		{
			if (_bgTrame)
			{
				_bgTrame.graphics.clear();
				s.removeChild(_bgTrame);
				_bgTrame = null;
			}
			
			_bgTrame = new Shape();
			
			var bd:BitmapData = new BitmapData(3, 3, true, 0);
			bd.setPixel32(0, 2, 0x11DDDDDD);
			bd.setPixel32(1, 1, 0x11DDDDDD);
			bd.setPixel32(2, 0, 0x11DDDDDD);
			
			_bgTrame.graphics.beginBitmapFill(bd);
			_bgTrame.graphics.drawRect(0, 0, w, h);
			_bgTrame.cacheAsBitmap = true;
			
			s.addChild(_bgTrame);
		}
		
		public static function playIn(mc:MovieClip)
		{
			mc["goto"] = mc.totalFrames;
			mc.addEventListener(Event.ENTER_FRAME, stepPlay);
		}
		
		public static function playOut(mc:Object)
		{
			mc["goto"] = 1;
			mc.addEventListener(Event.ENTER_FRAME, stepPlay);
		}
		
		public static function stepPlay(e:Event = null)
		{
			var f = e.target.currentFrame;
			if (f > e.target["goto"])
			{
				e.target.gotoAndStop( f-1 );
			}
			else if (f < e.target["goto"]) 
			{
				e.target.gotoAndStop(f+1);
			}
			else 
			{
				e.target.gotoAndStop(e.target["goto"]);
				e.target.removeEventListener(Event.ENTER_FRAME, stepPlay);
			}
		}
		
		public static function burn(ratio:Number):ColorTransform
		{
			var n:Number = 0xFF * ratio;
			return new ColorTransform(1, 1, 1, 1, n, n, n, 0);
		}
		
		public static function TweenBurn(dObj:DisplayObject, value:Number, duration:Number, begin:Number, d:Number=0)
		{
			// VALUE  0:NORMAL # -1:BLACK # 1:WHITE 
			
			var obj:Object = { value:begin };
			dObj.transform.colorTransform = burn(obj.value)
			
			obj.func = function()
			{
				dObj.transform.colorTransform = burn(obj.value)
			}
			
			TweenMax.to(obj,duration, { value:value, onUpdate:obj.func, delay:d} );
		}
		
		public static function TextFitSize(_t:TextField, _w:Number, _h:String = "+"):void
		{
			var _z:int;
			var _s:String = _t.text;
			
			_t.multiline = false;
			_t.autoSize = TextFieldAutoSize.CENTER;
			
			switch(_h)
			{
			case "+":
					
				_z = 5;
				
				while (_t.textWidth < _w)
				{
					_t.htmlText = "<font size='" + _z + "'>" + _s + "</font>";
					_z++;
				}
				
				_t.htmlText = "<font size='" + (_z - 2) + "'>" + _s + "</font>";
				
				break;
				
			case "-":
				
				_z = 127;
				
				while (_t.textWidth > _w)
				{
					_t.htmlText = "<font size='" + _z + "'>" + _s + "</font>";
					_z--;
				}
				
				_t.htmlText = "<font size='" + (_z + 1) + "'>" + _s + "</font>";
				
				break;
				
			}			
		}
		
		
		
		
		
	}
	
}
